package com.joelemon.mockinfluxdbdata.datamock.v2.mockserviceImpl;

import cn.hutool.core.date.DateTime;
import com.joelemon.mockinfluxdbdata.datamock.v1.ObjectModelMockConfig;
import com.joelemon.mockinfluxdbdata.datamock.v2.BaseMockConfig;
import com.joelemon.mockinfluxdbdata.enums.InsertType;
import com.joelemon.mockinfluxdbdata.mock.ConnectConfig;
import com.joelemon.mockinfluxdbdata.mock.DataLine;
import com.joelemon.mockinfluxdbdata.mock.InsertStrategy;
import com.joelemon.mockinfluxdbdata.mock.service.DBInsertService;
import com.joelemon.mockinfluxdbdata.mock.service.DBInsertServiceFactory;
import com.joelemon.mockinfluxdbdata.mock.service.DefaultMockHandleService;
import com.joelemon.mockinfluxdbdata.mock.service.MockHandleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 模拟数据
 * @Author: HJY
 * @Date: 2023/11/16
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BaseMockHandleService extends DefaultMockHandleService<BaseMockConfig> {

    private final DBInsertServiceFactory insertServiceFactory;

    @Override
    public Class handleClass() {
        return BaseMockConfig.class;
    }

    /**
     * 根据配置生成模拟数据
     *
     * @param mockConfig
     * @return
     */
    public List<DataLine> mock(BaseMockConfig mockConfig) {
        // 获取需要的时间
        List<DateTime> times = mockConfig.getInsertStrategy().getTimes();
        if (times != null && times.size() > 0) {
            return times.stream().flatMap(item -> mockConfig.getItems().stream()
                    .filter(mockItem -> mockItem != null && mockItem.check())
                    .map(mockItem -> {
                                DataLine dataLine = new DataLine();
                                Map<String, Object> fieldMap = mockItem.getFields().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
                                        entry -> entry.getValue().parseUnit().mockValue().getVal()));
                                dataLine.setTagMap(mockItem.getTags());
                                dataLine.setFieldMap(fieldMap);
                                dataLine.setMeasurement(mockConfig.getMeasurement());
                                dataLine.setTime(item);
                                return dataLine;
                            }
                    )).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 插入数据
     *
     * @param connectConfig
     * @param dataLines
     * @param insertType
     * @return
     */
    public int insert(ConnectConfig connectConfig, List<DataLine> dataLines, String insertType) {
        if (insertType == null || insertType.length() == 0) insertType = DBInsertServiceFactory.HTTP;
        DBInsertService defaultService = insertServiceFactory.getService(insertType);
        return defaultService.insertData(connectConfig, dataLines);
    }
}
